package com.matrix.gallery.ui.wallpaper.transformer

import android.view.View
import androidx.viewpager.widget.ViewPager
import kotlin.math.abs

class ZoomPageTransformer(alpha: Float) : ViewPager.PageTransformer {

    val MAX_SCALE: Float = 1.0f //0缩放
    val MIN_SCALE: Float = 0.85f //0.85缩放
    private var MIN_ALPHA = alpha //最小透明度

    override fun transformPage(view: View, position: Float) {

        //setScaleY只支持api11以上
        /**
         * (-oo,-1) 相对于左边第一页，其左边的所有页面 **
         * x、y拉伸为MIN_SCALE、透明度MIN_ALPHA
         */
        if (position < -1) {
            view.scaleX = MIN_SCALE
            view.scaleY = MIN_SCALE
            view.alpha = MIN_ALPHA
        } else if (position < 1) {
            val scaleFactor =
                (MIN_SCALE + (1 - abs(position.toDouble())) * (MAX_SCALE - MIN_SCALE)).toFloat()
            //[0, 1 ） 相对于当前选中页，其右边第一页 **
            if (position > 0) {
                view.translationX = -scaleFactor
            } else if (position < 0) {
                view.translationX = scaleFactor
            }
            view.scaleY = scaleFactor
            view.scaleX = scaleFactor

            // float alpha = 1f -  Math.abs(position) * (1 - );
            val alpha = (MIN_ALPHA + (1 - MIN_ALPHA) * (1 - abs(position.toDouble()))).toFloat()
            view.alpha = alpha
        } else { // (1,+Infinity]
            view.scaleX = MIN_SCALE
            view.scaleY = MIN_SCALE
            view.alpha = MIN_ALPHA
        }
    }
}